<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/2
 * Time: 17:08
 */

namespace Admin\Controller;


class YwController extends AdminController
{
    public function _myinitialize()
    {
        $tb_name=strtolower(CONTROLLER_NAME);
        $this->db=M($tb_name);
        $this->session_lst = CONTROLLER_NAME."lst";
    }

    public function lst()
    {
        $session_where = I('session_where');//不为空，则表明是从add/edit/del中跳转过来的，刚查询条件从session中取。
        $where['status']=1;
        if (empty($session_where)) {
            //筛选条件
            $xm = I('xm');
            $yw = I('yw');
            $zh = I('zh');
            $lj = I('lj');
            $zt = I('zt');
            $sy = I('sy');
            $xy = I('xy');
            $qy = I('qy');
            if (!empty($xm)) $where["xm"] = $xm;//项目
            if (!empty($yw)) $where["yw"] = $yw;//业务
            if (!empty($zh)) $where["zh"] = $zh;//账号
            if (!empty($lj)) $where["lj"] = $lj;//链接
            if (!empty($zt)) $where['zt'] = $zt;//上下线
            if (!empty($sy)) $where["sy"] = $sy;//上游
            if (!empty($xy)) $where["xy"] = $xy;//下游
            if (!empty($qy)) $where["qy"] = $qy;//区域

            //上线时间和下线时间
            $sxsj_start = I('sxsj_start');//上线开始
            $sxsj_end = I('sxsj_end');//上线结束
            $xxsj_start = I("xxsj_start");//下线开始
            $xxsj_end = I("xxsj_end");//下线结束

            if (!empty($sxsj_start) && !empty($sxsj_end)) {
                $where['sxsj'] = array("between", array($sxsj_start, $sxsj_end));
            }

            if (!empty($sxsj_start) || !empty($sxsj_end)) {
                if (!empty($sxsj_start)) $where['sxsj'] = $sxsj_start;
                if (!empty($sxsj_end)) $where['sxsj'] = $sxsj_end;
            }

            if (!empty($xxsj_start) && !empty($xxsj_end)) {
                $where['xxsj'] = array("between", array($xxsj_start, $xxsj_end));
            }

            if (!empty($xxsj_start) || !empty($xxsj_end)) {
                if (!empty($xxsj_start)) $where['xxsj'] = $xxsj_start;
                if (!empty($xxsj_end)) $where['xxsj'] = $xxsj_end;
            }


            //将查询条件存入session中，方便更新时跳转
            session(CONTROLLER_NAME . "where", $where);
        } else {
            $where = session(CONTROLLER_NAME . "where");
        }
        C('LIST_ROWS',200);
        $lists = $this->lists($this->db, $where, array("zt"=>"asc","updatetime"=>"desc"));
        session($this->session_lst,null);
        session($this->session_lst,$lists);
        $this->assign('lists', $lists);

        //下拉
        $map['status']=1;
        $xms=M('xm')->where($map)->select();
        $yws=M('ywn')->where($map)->select();
        $sys=M('sy')->where($map)->select();
        $xys=M('xy')->where($map)->select();
        $qys=M('qy')->where($map)->select();

        $this->assign('xms',$xms);
        $this->assign('yws',$yws);
        $this->assign('sys',$sys);
        $this->assign('xys',$xys);
        $this->assign('qys',$qys);

        $this->display();
    }

    public function add()
    {
        if (IS_POST) {
            $data = $this->db->create();
            if ($data === false) {
                $this->error($this->db->getError());
            } else {
                $data['xm_id'] = date("YmdHis");
                $data['updatetime'] = time();
            }

            $rid = $this->db->add($data);

            if ($rid === false) {
                $this->error($this->db->getDbError());
            }

            $this->redirect('lst', array("session_where" => "session_where"));
        }
    }

    public function edit()
    {
        if (IS_POST) {
            $data = $this->db->create();
            if ($data === false) {
                $this->error($this->db->getError());
            } else {
                $data['xm_id'] = date("YmdHis");
                $data['updatetime'] = time();

                $rid = $this->db->save($data);

                if ($rid === false) {
                    $this->error($this->db->getDbError());
                }
                $this->redirect('lst', array("session_where" => "session_where"));
            }
        } else {
            $id = I('id');
            if (!empty($id)) {
                $data = $this->db->where(array("id" => $id))->find();
                $this->ajaxReturn($data);
            }
        }
    }

    public function del()
    {
        $id = I('id');
        if (!empty($id)) {
            $data['status'] = 0;
            $data['updatetime'] = time();
            $this->db->where(array('id' => $id))->setField($data);
            $this->redirect('lst', array("session_where" => "session_where"));
        }

    }

    public function csv_export(){
        $this->export();
    }

}